  var CryptoJS = require('crypto-js');
  //前端加密方法：
function  encryptString(name) {
    var str = name; //需要加密的字符串
    var aesKey = 'WKXZi1c3b1e8OhUm'; //秘钥
    var key = CryptoJS.enc.Utf8.parse(aesKey); //将秘钥转换成Utf8字节数组
    //		srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(str));
    var encrypted = CryptoJS.AES.encrypt(str, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    var jiami = encrypted.ciphertext.toString().toUpperCase();
    return jiami;
  }

  //前端解密方法：
function  decodeString(data) {
    //res.data是需要解密的字符串
    var aesKey = 'WKXZi1c3b1e8OhUm'; //秘钥
    var key = CryptoJS.enc.Utf8.parse(aesKey); //将秘钥转换成Utf8字节数组
    var encryptedHexStr = CryptoJS.enc.Hex.parse(data);
    var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    var decrypt = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    var jiemi = decrypt.toString(CryptoJS.enc.Utf8);
    return JSON.parse(jiemi);
  }
  //获取token
  function  syncGettoken() {
	  var user = wx.getStorageSync("user")
    return new Promise((resolve, reject) => {
      uni.request({
        url: "https://gftjis.jxzwfww.gov.cn/jxtyyh/interfaces/toBank/userQuickLogin.do",
        dataType: "json",
        type: "post",
        data: {
          name: encryptString(user.realName),
          idCard:  encryptString(user.identityNumber)
        },
        success: function(result) {
          if(result.data.result == true) {
            resolve( syncGetticket(result.data.token))
          }
        },
        error: function(err) {
          reject(err)
        }
      });
   })
    
  }
  //token转换ticket
  function syncGetticket(ticket) {
	  var tic;
    return new Promise((resolve, reject) => {
      uni.request({
        url: "https://gftjis.jxzwfww.gov.cn/jxtyyh/interfaces/jis/getTicket.do",
        dataType: "json",
        type: "post",
        data: {
          token: ticket
        },
        success: function(result) {
          if(result.data.result == true) {
			  tic = result.data.ticket;
            resolve(result.data.ticket)
          }
        },
        error: function(err) {
          reject(err)
        }
      });
    })
  }
  function getFace(name,idCard,phone){
		  wx.startFacialRecognitionVerify({
			name:name,
			idCardNumber:idCard,
			success(){
				wx.setStorageSync("name",name)
				wx.setStorageSync("idCard",idCard)
				wx.setStorageSync("phone",phone)
				wx.setStorageSync("isFacePass",1)
				getApp().globalData.isLogin="1";
				// uni.switchTab({url: '/pages/tabBar/index/index'})
			},
			fail(res){
				
			}
	  })
  }
  
module.exports = {
  syncGettoken,
  getFace
}